const {
	http
} = require("../../../utils/apiutils");
import util from '../../../utils/util'
// pages/home/risk/index.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: null,
		queryParams: {
			keyword: ''
		},
		pageNo: 1,
		pageSize: 10,
		loading: false,
		hasMore: true,
		listdata: [],
		jobName: '',
		fxtype:2,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			id: options.id,
			jobName:options.jobName
		})
		wx.setNavigationBarTitle({
		  title: options.jobName+'-风险评估',
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.data.pageNo = 1;
		this.data.loading = false;
		this.data.hasMore = true;
		this.data.listdata = [];
		if (this.data.fxtype == 1) {
			this.getRzList()
		} else { 
			this.getList();
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},
	search(e) {
		this.data.queryParams.keyword = e.detail;
		this.data.pageNo = 1;
		this.data.listdata = [];
		this.data.loading = false;
		this.data.hasMore = true;
		if (this.data.fxtype == 1) {
			this.getRzList();
		} else { 
			this.getList();
		}
	},
	getList() {
		let params = {
			jobId: this.data.id,
			keyword: this.data.queryParams.keyword,
			pageNo: this.data.pageNo,
			pageSize: this.data.pageSize,
			fxtype:this.data.fxtype
		}
		if (this.data.fxtype == 1) { 
			this.setData({
				listdata:[]
			})
			return false;
		}
		if (this.data.loading || !this.data.hasMore) return false;
		http.getRiskUserListByJobId(params, res => {
			if (res.status == 200) {
				let data = [...res.obj.records];
				console.log(util)
				if(data.length>0){
					data.map(item=>{
						item.birthdayInAge = item.idNumber?util.getAge(item.idNumber)+'岁':''
					})
				}
				this.setData({
					listdata: [...this.data.listdata, ...data],
					pageNo: this.data.pageNo + 1,
					loading: false,
					hasMore: res.obj.records.length > 0 ? true : false
				})
			} else {
				wx.showToast({
					title: res.msg || res.message,
					icon: "none"
				})
			}
		}, err => {})
	},
	getRzList() {
		let params = {
			jobId: this.data.id,
			keyword: this.data.queryParams.keyword,
			pageNo: this.data.pageNo,
			pageSize: this.data.pageSize,
		}
		if (this.data.loading || !this.data.hasMore) return false;
		http.getUserActivity(params, res => {
			if (res.status == 200) {
				let data = [...res.obj.records];
				console.log(util)
				if(data.length>0){
					data.map(item=>{
						item.birthdayInAge = item.idNumber?util.getAge(item.idNumber)+'岁':''
					})
				}
				this.setData({
					listdata: [...this.data.listdata, ...data],
					pageNo: this.data.pageNo + 1,
					loading: false,
					hasMore: res.obj.records.length > 0 ? true : false
				})
			} else {
				wx.showToast({
					title: res.msg || res.message,
					icon: "none"
				})
			}
		}, err => {})
	},
	changefx() {
		this.data.pageNo = 1;
		this.data.listdata = [];
		this.data.loading = false;
		this.data.hasMore = true;
		if (this.data.fxtype == 1) {
			this.getRzList();
		} else { 
			this.getList();
		}
	 },
	todtl(e){
		wx.navigateTo({
		  url: '/pages/home/assessment-dtl/index?userId='+e.currentTarget.dataset.userId+"&jobId="+this.data.id,
		})
	}
})